package com.tadpole.music.view.activity.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.tadpole.music.R;
import com.tadpole.music.app.MyApplication;
import com.tadpole.music.bean.PushBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.presenter.me.LoginOutPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.MainActivity;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.web.H5Activity;
import com.tadpole.music.view.widget.LoginOutPopup;

/**
 * 设置
 * Created By 孙岩
 * on 2020/9/12
 */
public class SettingActivity extends BaseActivity implements RegisterIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //修改密码
    private RelativeLayout rlUpdatePwd;
    //意见反馈
    private RelativeLayout rlFanKui;
    //退出登录
    private RelativeLayout rlLoginOut;
    //隐私政策
    private TextView tvZhengCe;
    //联系客服
    private RelativeLayout rlKeFu;
    //申请注销
    private RelativeLayout rlZhuxiao;
    private LoginOutPresenter loginOutPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initViews();
        initListeners();
        loginOutPresenter = new LoginOutPresenter();
        loginOutPresenter.attachView(this);

    }

    //初始化控件
    private void initViews() {
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("设置");
        rlUpdatePwd = findViewById(R.id.rlUpdatePwd);
        rlFanKui = findViewById(R.id.rlFanKui);
        rlLoginOut = findViewById(R.id.rlLoginOut);
        tvZhengCe = findViewById(R.id.tvZhengCe);
        rlKeFu = findViewById(R.id.rlKeFu);
        rlZhuxiao = findViewById(R.id.rlZhuxiao);
    }

    //初始化监听器
    private void initListeners() {
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                SettingActivity.this.finish();
            }
        });

        //修改密码
        rlUpdatePwd.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(SettingActivity.this).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(SettingActivity.this, UpdatePasswordActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //意见反馈
        rlFanKui.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                Intent intent = new Intent(SettingActivity.this, FanKuiActivity.class);
                startActivity(intent);
            }
        });

        //注销账号
        rlZhuxiao.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                Intent intent = new Intent(SettingActivity.this, UserCancelActivity.class);
                startActivity(intent);
            }
        });

        //退出登录
        rlLoginOut.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(SettingActivity.this).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        final LoginOutPopup popup = new LoginOutPopup(SettingActivity.this);
                        popup.setPopupWindowFullScreen(true);
                        popup.showPopupWindow();
                        popup.setModeListener(new LoginOutPopup.IModeSelection() {
                            @Override
                            public void getMode(int mode) {
                                if (mode == 1) {//确定
                                    popup.dismiss();
                                    loginOutPresenter.loginOut();
                                } else if (mode == 2) {//取消
                                    popup.dismiss();
                                }
                            }
                        });
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //隐私政策
        tvZhengCe.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    PushBean pushBean = new PushBean();
                    pushBean.setUrl("https://kedou.aiyuechen.com/api/v2/notice/81");
                    pushBean.setTitle("隐私政策");
                    Intent intent = new Intent(SettingActivity.this, H5Activity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("params",pushBean);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //联系客服
        rlKeFu.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                callPhone("15600991576");
            }
        });
    }

    /**
     * 拨打电话（直接拨打电话）
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    @Override
    public void showResult() {
        ToastUtils.show("退出登录成功");
        //清空Sp保存
        SpUtil.getInstance(SettingActivity.this).clear();
        SpUtil.getInstance(this).putString(Constant.FREE_COURSE,"1");
        MyApplication.getInstance().finishManager.removeAllActivity();
        Intent intent = new Intent(SettingActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void show401() {
        this.finish();
        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(this).putString(Constant.TOKEN, "");
    }
}
